package com.tjshop.android.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.tjshop.android.AddAddressActivity;
import com.tjshop.android.R;
import com.tjshop.android.model.tj.json.Address;
import com.tjshop.android.util.SharedPreferencesUtil;

import java.util.List;

/**
 * Created by gytaiSSD on 2014/12/29.
 */
public class AddressListAdapter extends BaseAdapter implements OnCheckedChangeListener,OnClickListener {
	List<Address> address;
	private Context mContext;
	public AddressListAdapter(Context context, List<Address> clist) {
		address = clist;
		this.mContext = context;
	}

	@Override
	public int getCount() {
		return address.size();
	}

	@Override
	public Object getItem(int i) {
		return address.get(i);
	}

	@Override
	public long getItemId(int i) {
		return i;
	}

	@Override
	public View getView(final int i, View convertView, ViewGroup viewGroup) {
		  ViewHolder viewHolder = null;
	        if (null == convertView)
	        {
	            viewHolder = new ViewHolder();
	            convertView = LayoutInflater.from(mContext).inflate(
						R.layout.activity_address_list_item, null);

	            viewHolder.delete = (Button) convertView.findViewById(R.id.delete);
	            viewHolder.edit = (Button) convertView.findViewById(R.id.edit);
	            viewHolder.select = (CheckBox) convertView.findViewById(R.id.select);
	            viewHolder.selectAddress = (LinearLayout) convertView.findViewById(R.id.selectAddress);
	            viewHolder.name = (TextView) convertView.findViewById(R.id.add_name);
	            viewHolder.tel = (TextView) convertView.findViewById(R.id.add_tel);
	            viewHolder.address = (TextView) convertView.findViewById(R.id.address);
	            viewHolder.ll_select = (LinearLayout) convertView.findViewById(R.id.ll_select);
	            convertView.setTag(viewHolder);
	        }
	        else
	        {
	            viewHolder = (ViewHolder) convertView.getTag();
	        }
	        
	        Address add = address.get(i);
	        viewHolder.address.setText(add.getAddress());
	        viewHolder.name.setText(add.getConsignee());
	        viewHolder.tel.setText(add.getTel());
	        
	        String id = SharedPreferencesUtil.get(mContext, "config", "default_address");
	        if(id != null && add.getAddress_id().equals(id)){
	        	viewHolder.select.setChecked(true);
	        	viewHolder.select.setText("  默认地址");
	        	viewHolder.select.setTextColor(Color.RED);
	        }else{
	        	viewHolder.select.setChecked(false);
	        	viewHolder.select.setText("  设为默认地址");
	        	viewHolder.select.setTextColor(Color.BLACK);
	        }
//	        viewHolder.select.setTag(add);
	        
	        viewHolder.ll_select.setTag(add);
	        viewHolder.ll_select.setOnClickListener(this);
	        
//	        viewHolder.select.setOnCheckedChangeListener(this);
	        viewHolder.delete.setTag(add);
	        viewHolder.delete.setOnClickListener(this);
	        viewHolder.edit.setTag(add);
	        viewHolder.edit.setOnClickListener(this);
	        viewHolder.selectAddress.setTag(add);
	        viewHolder.selectAddress.setOnClickListener(this);
	        
	        return convertView;
	}
	private static class ViewHolder
    {
        CheckBox select;
        Button delete,edit;
        LinearLayout selectAddress,ll_select;
        TextView name,tel,address;
        
    }
	@Override
	public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
		Address add = (Address) arg0.getTag();
		if(arg1){
			arg0.setText("  默认地址");
			arg0.setTextColor(Color.RED);
			SharedPreferencesUtil.save(mContext, "config", "default_address", add.getAddress_id());
		}else{
			arg0.setText("  设为默认地址");
			arg0.setTextColor(Color.BLACK);
		}
	}



	@Override
	public void onClick(View arg0) {
		Address add = (Address) arg0.getTag();
		if(arg0.getId() == R.id.delete){
			if(add.delete(mContext)){
				address.remove(add);
				notifyDataSetChanged();
			}
		}else if(arg0.getId() == R.id.selectAddress){
			Intent i = new Intent(mContext, AddAddressActivity.class);
			Bundle b = new Bundle();
			b.putString("type", "view");
			b.putSerializable("data", add);
			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			i.putExtras(b);
			mContext.startActivity(i);
		}else if(arg0.getId() == R.id.edit){
			Intent i = new Intent(mContext, AddAddressActivity.class);
			Bundle b = new Bundle();
			b.putString("type", "edit");
			b.putSerializable("data", add);
			i.putExtras(b);
			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			mContext.startActivity(i);
		}else if(arg0.getId() == R.id.ll_select){
			SharedPreferencesUtil.save(mContext, "config", "default_address", add.getAddress_id());
			notifyDataSetChanged();
		}
	}
	
}
